package cn.ty.znsz.Login;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;

/**
 * 注册验证参数
 */
public class RegisterValidator extends Validator{
	
	@Inject
	LoginService ls;
	
	protected void validate(Controller c) {
		setShortCircuit(true);
		setRet(Ret.fail());		// Ret.fail() 将设置 state : "fail" 值
		
		validateRequired("userName", "msg", "昵称不能为空");
		validateString("userName", 1, 19, "msg", "昵称不能超过19个字");
		validateRequired("loginName", "msg", "账号不能为空");
		validateRequired("password", "msg", "密码不能为空");
		validateRequired("repassword", "msg", "请重复输入密码");
		//正则验证密码(包含字母，数字和特殊字符，且长度在8位以上)
		String reg="^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
		validateRegex("password", reg, false,"msg", "密码应包含字母，数字和特殊字符，且长度在8位以上");
		//验证两个密码是否相等
		validateEqualField("password", "repassword", "msg", "两次输入密码不一致");
		validateCaptcha("captcha", "msg", "验证码不正确");
		//验证账号是否已经被注册
		if(ls.checkLoginName(c.getPara("loginName"))) {
			addError("msg", "账号已被注册");
		};
	}

	@Override
	protected void handleError(Controller c) {
		// getRet() 与 setRet(...) 配合使用
		Ret ret = getRet();
		c.renderJson(ret);		
	}
}
